package com.hhit.zhgd.service.impl

import cn.afterturn.easypoi.excel.ExcelImportUtil
import cn.afterturn.easypoi.excel.entity.ImportParams
import cn.hutool.core.collection.CollectionUtil
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.hhit.zhgd.dao.ZhgdAwardManagementMapper
import com.hhit.zhgd.domain.entity.ZhgdAwardManagement
import com.hhit.zhgd.domain.entity.ZhgdRiskPlan
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdAwardManagementService
import com.hhit.zhgd.service.ZhgdRiskPlanMapper
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.util.*

@Service("zhgdAwardManagementService")
class ZhgdAwardManagementServiceImpl : ZhgdAwardManagementService,ServiceImpl<ZhgdAwardManagementMapper,ZhgdAwardManagement>() {

    @Autowired
    private val tjAwardManagementMapper: ZhgdAwardManagementMapper? = null

    @Autowired
    private val tjRiskPlanMapper: ZhgdRiskPlanMapper? = null

    override fun selectAll(tJjAwardManagement: ZhgdAwardManagement?): IPage<ZhgdAwardManagement?> {
        val wrapper = QueryWrapper<ZhgdAwardManagement>()

        if (StringUtils.isNotBlank(tJjAwardManagement?.keyWord)) {
            wrapper.like("key_word", tJjAwardManagement?.keyWord)
                .or().like("project_package_name", tJjAwardManagement?.keyWord)
        }
        val pageRequest = Page<ZhgdAwardManagement>(
            tJjAwardManagement?.page?.toLong()!!, tJjAwardManagement?.size?.toLong()!!
        )
        val zhgdAwardManagementIPage: IPage<ZhgdAwardManagement?> = this.page(pageRequest, wrapper)

        for (jjAwardManagement in zhgdAwardManagementIPage.records) {
            val projectNameList: MutableList<String?> = ArrayList()
            projectNameList.add(jjAwardManagement?.projectName)
            val zhgdRiskPlans: List<ZhgdRiskPlan>? = tjRiskPlanMapper?.selectList(
                QueryWrapper<ZhgdRiskPlan>().eq(
                    "related_id",
                    jjAwardManagement?.id
                )
            )
            jjAwardManagement?.certificateApplicationConfigClusterList = zhgdRiskPlans
        }

        return zhgdAwardManagementIPage
    }

    override fun insert(tJjAwardManagement: ZhgdAwardManagement?): Int? {
        return tjAwardManagementMapper?.insert(tJjAwardManagement)
    }

    override fun delete(idList: List<Long?>?): Int {
        return tjAwardManagementMapper?.deleteBatchIds(idList)?:0
    }

    override fun importExcel(file: MultipartFile?): BaseResp<Any?>? {
        val params = ImportParams()
        params.titleRows = 1
        params.headRows = 2
        params.isNeedSave = false
        val list = ExcelImportUtil.importExcel<ZhgdAwardManagement?>(
            file!!.inputStream,
            ZhgdAwardManagement::class.java, params
        )
        if (CollectionUtil.isNotEmpty(list)) {
            for (tJjAwardManagement in list) {
                tjAwardManagementMapper?.insert(tJjAwardManagement)
                val tJjRiskPlanList: List<ZhgdRiskPlan?>? = tJjAwardManagement!!.certificateApplicationConfigClusterList
                if (CollectionUtil.isNotEmpty(tJjRiskPlanList)) {
                    for (tJjRiskPlan in tJjRiskPlanList!!) {
                        tJjRiskPlan?.id = UUID.randomUUID().toString()
                        tJjRiskPlan?.relatedId = tJjAwardManagement?.id
                        tjRiskPlanMapper?.insert(tJjRiskPlan)
                    }
                }
            }
        }

        return BaseResp.success("成功")
    }

    override fun findTJjRiskPlanList(tJjAwardManagement: ZhgdAwardManagement?): List<ZhgdRiskPlan?>? {
        return tjAwardManagementMapper?.findTJjRiskPlanList(tJjAwardManagement)
    }

    override fun deleteTJjRiskPlan(ids: List<String?>?): BaseResp<Any?>? {
        if (CollectionUtil.isNotEmpty(ids)) {
            tjAwardManagementMapper?.deleteBatchIds(ids)
            for (id in ids!!) {
                tjRiskPlanMapper?.delete(QueryWrapper<ZhgdRiskPlan>().eq("related_id", id))
            }
        }
        return BaseResp.success()
    }
}